<?php
namespace app\modules\cn\models;
use yii\db\ActiveRecord;
use app\libs\Method;
use yii;
class User extends ActiveRecord {
    public $cateData;

    public static function tableName(){
        return '{{%user}}';
    }

    public static function getUsername($userId){
        $name= Yii::$app->db->createCommand("select * from {{%user}} where id=$userId")->queryAll();
        $uname=$name[0]['nickname'];
        if($uname==null){
           $uname=$name[0]['userName'];
        }
        return $uname;
    }

    public static function saveUserMessage($uid){
        $userData =  User::find()->asArray()->where('uid='.$uid)->one();// 帖子内容的id
        $data = json_decode(Method::post("https://login.thinkwithu.com/cn/ssl-api/user-information", ['uid' => $uid]), true);
        if(isset($data['source']))unset($data['source']);
        if(isset($data['regdate']))unset($data['regdate']);
        $data['userName'] = $data['username'];
        $data['createTime'] = time();
        unset($data['username']);
        if($userData==false) {
            $re = Yii::$app->db->createCommand()->insert("{{%user}}", $data)->execute();
            return $re;
        }else{
            if($userData['nickname']!=$data['nickname']){
                $re = User::updateAll(['nickname' => $data['nickname']], "uid=".$data['uid']);
                return $re;
            }
        }
    }
}